package com.yuyou.train.user.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.yuyou.train.service.api.user.dto.MyCommentPageDTO;
import com.yuyou.train.service.api.user.dto.UserCommentPageDTO;
import com.yuyou.train.service.api.user.dto.UserLessonCommentDTO;
import com.yuyou.train.service.api.user.entity.UserComment;
import com.yuyou.train.service.api.user.entity.UserPraise;
import com.yuyou.train.user.mapper.UserCommentMapper;
import com.yuyou.train.user.service.IUserCommentService;
import com.yuyou.train.user.service.IUserPraiseService;
import com.yuyou.train.user.service.IUserTotalService;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.enums.user.UserCommentEnums;
import com.yuyou.train.util.tool.Sequence;

@Service
public class UserCommentServiceImpl implements IUserCommentService{

	@Autowired
	private UserCommentMapper userCommentMapper;

	@Autowired
	private IUserPraiseService userPraiseService;
	
	@Autowired
	private IUserTotalService userTotalService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userCommentMapper.deleteByPrimaryKey(id);
	}

	@Override
	@Transactional(timeout=60)
	public void deleteCommentWithChildren(String id){
		userCommentMapper.deleteByPrimaryKey(id);
		userCommentMapper.deleteByParentId(id);
	}

	@Override
	public int insert(UserComment record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return userCommentMapper.insert(record);
	}

	@Override
	public UserComment selectByPrimaryKey(String id) {
		return userCommentMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserComment record) {
		record.setUpdateTime(new Date());
		return userCommentMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	@Transactional(timeout=60)
	public int addUserComment(UserComment userComment) throws Exception {	
		userTotalService.updatecThHwTotal(userComment.getUserId());
		return this.insert(userComment);
	}

	@Override
	public List<UserLessonCommentDTO> findUserComment(UserComment record) {
		return userCommentMapper.findUserComment(record);
	}

	@Override
	public List<UserCommentPageDTO> findUserCommentPageList(Pagination<UserCommentPageDTO> p) {
		return userCommentMapper.findUserCommentPageList(p);
	}

	@Override
	public int findUserCommentPageCount(Pagination<UserCommentPageDTO> p) {
		return userCommentMapper.findUserCommentPageCount(p);
	}

	@Override
	public int sumStar(Pagination<UserCommentPageDTO> p) {
		Integer sumStar = userCommentMapper.sumStar(p);
		return null == sumStar ? 0 : sumStar;
	}

	@Override
	public List<UserComment> finduserCommentLimit(String foreignId, int start,
			int limit) {
		return userCommentMapper.finduserCommentLimit(foreignId, start, limit);
	}


	@Override
	@Transactional(timeout=60)
	public int verComment(List<String> ids, String verifyStatus) {
		for(String id:ids){
			UserComment userComment=new UserComment();
			userComment.setId(id);
			userComment.setVerifyStatus(verifyStatus);
			this.updateByPrimaryKeySelective(userComment);
		}
		return 1;
	}

	@Override
	public int findUserLessonCommentPageCount(Pagination<UserLessonCommentDTO> p) {
		return userCommentMapper.findUserLessonCommentPageCount(p);
	}

	@Override
	public List<UserLessonCommentDTO> findUserLessonCommentPageList(Pagination<UserLessonCommentDTO> p, String userId) {
		List<UserLessonCommentDTO> userLessonCommentList = userCommentMapper.findUserLessonCommentPageList(p);
		// 查询子评论
		for(UserLessonCommentDTO userLessonComment : userLessonCommentList){
			UserComment userCommentQuery = new UserComment();
			userCommentQuery.setParentId(userLessonComment.getId());
			userCommentQuery.setVerifyStatus(UserCommentEnums.verifyStatus.success.getValue());
			List<UserLessonCommentDTO> children = userCommentMapper.findUserComment(userCommentQuery);
			userLessonComment.setChildren(children);

			// 评论数量
			userLessonComment.setCommentSum(CollectionUtils.isEmpty(children) ? 0 : children.size());

			// 点赞数
			List<UserPraise> userPraiseList = userPraiseService.findUserPraiseByForeignId(userLessonComment.getId());
			userLessonComment.setPraise(CollectionUtils.isEmpty(userPraiseList) ? 0 : userPraiseList.size());

			// 是否点赞
			UserPraise userPraise = userPraiseService.findPraiseByForeignIdAndUserId(userLessonComment.getId(), userId);
			userLessonComment.setIfPraise(null == userPraise ? false : true);
		}

		return userLessonCommentList;
	}


	@Override
	public int findMyCommentPageCount(Pagination<MyCommentPageDTO> p) {
		return userCommentMapper.findMyCommentPageCount(p);
	}

	@Override
	public int findCommentOnMePageCount(Pagination<MyCommentPageDTO> p) {
		return userCommentMapper.findCommentOnMePageCount(p);
	}

	@Override
	public List<MyCommentPageDTO> findMyCommentPage(Pagination<MyCommentPageDTO> p) {
		return userCommentMapper.findMyCommentPage(p);
	}

	@Override
	public List<MyCommentPageDTO> findCommentOnMePage(Pagination<MyCommentPageDTO> p) {
		return userCommentMapper.findCommentOnMePage(p);
	}

	@Override
	public Long countByForeignId(String foreignId) {
		return userCommentMapper.countByForeignId(foreignId);
	}
}